<template>
  <div>
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      :default-sort = "sortRule"
      @selection-change="selectionChange"
      :stripe="hasStripe"
      :border="hasBorder"

      ref="myTable"
    >
      <!-- 复选框列 -->
      <el-table-column type="selection" width="55" v-if="hasSelection" fixed>
      </el-table-column>
      <!-- 序号列 -->
      <el-table-column type="index" width="50" v-if="hasIndex" label="序号" fixed>
      </el-table-column>

      <template v-for="item in tableColumnOptions">
        <!-- 插槽列 -->
        <el-table-column
          v-if="item.isSlot"
          :sortable="item.isSortable"
          :key="item.label"
          :prop="item.prop"
          :label="item.label"
          :width="item.width || 110"
          :header-align="item.headerAlign || 'center'"
          :align="item.align || 'center'"
          :fixed="item.fixed"
          
        >
          <template slot-scope="{ row }">
            <slot :name="item.slotName" :data="row"></slot>
          </template>
        </el-table-column>

        <!-- 非插槽列 -->
        <el-table-column
          v-else
          :key="item.label"
          :sortable="item.isSortable"
          :prop="item.prop"
          :label="item.label"
          :min-width="item.width || 100"
          :header-align="item.headerAlign || 'center'"
          :align="item.align || 'center'"
        >
        </el-table-column>
      </template>
    </el-table>
    <el-row v-if="isHasButton">
      <el-col>
        <el-button type="parmary" @click="sureAdd" size="mini"
          >确定添加</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12" :offset="9">
        <el-pagination
          v-if="hasPagination"
          background
          @current-change="hanldCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          layout=" prev, pager, next, jumper,total"
          :total="total"
          class="pagination"
        >
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "myTable",
  props: {
    // 表格数据
    tableData: {
      type: Array,
      default() {
        return [];
      },
    },

    // 表头配置项
    tableColumnOptions: {
      type: Array,
      default() {
        return [];
      },
    },

    // 是否带有边框
    hasBorder: {
      type: Boolean,
      default: true,
    },

    // 是否带有序号
    hasIndex: {
      type: Boolean,
      default: true,
    },
    // 是否带有复选框
    hasSelection: {
      type: Boolean,
      default: false,
    },
    // 是否带有斑马纹
    hasStripe: {
      type: Boolean,
      default: true,
    },
    // 是否含有分页器
    hasPagination: {
      type: Boolean,
      default: true,
    },

    isHasButton: {
      type: Boolean,
      default: false,
    },
    isSortable: {
      type: Boolean,
      default: true,
    },
    isFilterCheckbox: {
      type: Boolean,
      default: false,
    },
    sortRule: {
      type: Object,
      default: null,
    }
  },
  data() {
    return {
      pageSize: 15,
      currentPage: 1,
      multipleSelection: [],
    };
  },
  methods: {
    selectionChange(val) {
      this.$emit("selectionChange", val);
    },
    hanldCurrentChange(current) {
      this.currentPage = current;
    },
    sureAdd() {
      this.$emit("sureAdd");
      this.multipleSelection = cloneDeep();
      this.$refs.myTable.clearSelection();
    },
  },
  computed: {
    total() {
      return this.tableData.length;
    },
  },
};
</script>

<style>
.el-table .el-table__cell {
  padding: 5px 0 !important;
}
</style>